package org.roguelikedevelopment.dweller.j2se;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.roguelikedevelopment.dweller.common.application.DataStorage;
import org.roguelikedevelopment.dweller.common.util.StorableData;
import org.roguelikedevelopment.dweller.common.util.logger.Logger;

public class SaveFileStorage implements DataStorage {

	private String saveFolder;
	
	public SaveFileStorage() {
		String userhome = System.getProperty("user.home", ".");
		String dwellerPath = userhome + File.separator + "dweller";
		String savePath = dwellerPath + File.separator + "save";
		File dwellerdir = new File(dwellerPath);
		if(!dwellerdir.isDirectory()) {
			dwellerdir.mkdir();
		}
		File savedir = new File(savePath);
		if(!savedir.isDirectory()) {
			savedir.mkdir();
		}
		saveFolder = savePath + File.separator;
	}
	
	@Override
	public boolean delete(String name) {
		Logger.debug("SaveFileStorage.delete() " + name);
		File file = new File(saveFolder + name);
		Logger.debug("SaveFileStorage.delete() " + file.getAbsolutePath());
		return file.delete();
	}

	@Override
	public boolean exists(String name) {
		System.out.println("SaveFileStorage.exists() " + name);
		File file = new File(saveFolder + name);
		System.out.println("SaveFileStorage.exists() " + file.exists());
		return file.exists();
	}

	@Override
	public long getAvailableSize() {
		File file = new File(saveFolder + ".");
		return file.getFreeSpace();
	}

	@Override
	public String[] getDataList() {
		String[] list = null;
		File file = new File(saveFolder);
		File[] files = file.listFiles();
		if(files != null) {
			int filecount = 0;
			for(File f : files) {
				if(f.isFile()) {
					filecount++;
				}
			}
			if(filecount > 0) {
				list = new String[filecount];
				int index = 0;
				for(File f : files) {
					if(f.isFile()) {
						list[index++] = f.getName();
					}
				}
			}			
		}
		return list;
	}

	@Override
	public long getSize(String name) {
		File file = new File(saveFolder + name);
		return file.length();
	}

	@Override
	public StorableData load(String name, boolean decompress) {
		File file = new File(saveFolder + name);
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		DataInputStream dis = null;
		try {
			fis = new FileInputStream(file);
			bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);
		}
		catch(Exception e) {
			System.err.println(e);
		}
		return new StorableData(dis);
	}

	@Override
	public void store(String name, StorableData data, boolean compress) {
		File file = new File(saveFolder + name);
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		DataOutputStream dos = null;
		try {
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			dos = new DataOutputStream(bos);
			
			Logger.debug("written bytes = " + data.getWrittenBytes());
			dos.write(data.getWrittenBytes());
			dos.flush();
			
			fos.close();
			bos.close();
			dos.close();
		}
		catch(Exception e) {
			System.err.println(e);
		}
	}

}
